<?php

function ImExport($bot)
{
	if($bot->firstrun)
	{
		//not work if it's first bot cycle
		//$Name = "buildings"; $Version = "0.9"; $Date = "2011-02-09";
		//$bot->ld->UpdatePluginVersion($bot, $Name, $Version, $Date )  ;
		return;
	}

	//getting plugin configurations
	$data = $bot->ld->GetPlSettings("im-export");
	if( ! isset($data))
	return;

	$msg = $bot->irc('u') . $bot->irc('b');
	$msg .= $bot->irc('cc') . '2';
	$msg .= $bot->irc('ff') . '13,5' . chr(232) . $bot->irc('ff') . '14,5';
	$msg .= 'Starting Plugin: CV IM-EXPORT';
	$bot->SendMsg($msg);

	//==========================================================================
	$arcargoContracts = array(
	"paris_plane" => 5 * 60, //5mins
	"barcelona_plane" => 6 * 60 * 60, //6 hours
	"shanghai_plane" => 12 * 60 * 60, //12 hours
	"tokyo_plane" => 18 * 60 * 60, //18 hours
	"london_plane" => 24 * 60 * 60 //24 hours		   		   
	);
	$arpassengerContracts = array(		   
	"rome_plane" => 1 * 60, //1mins
	"toronto_plane" => 1 * 60 * 60, //1 hours
	"new_york_plane" => 6 * 60 * 60, //6 hours
	"berlin_plane" => 8 * 60 * 60, //8 hours
	"rio_plane" => 12 * 60 * 60 //12 hours		   
	);
	$arvacationContracts = array(		   
	"cairo_plane" => 1 * 60 * 60, //1 hours
	"mumbai_plane" => 18 * 60 * 60, //18 hours
	"seoul_plane" => 24 * 60 * 60 //24 hours		   
	);	
	$artruckContracts = array(		   
	"trucking_frankfurt" => 1 * 60, //1mins
	"trucking_losangeles" => 1 * 60 * 60, //1 hours
	"trucking_zurich" => 4 * 60 * 60, //4 hours
	"trucking_atlanta" => 8 * 60 * 60, //8 hours
	"trucking_barcelona" => 12 * 60 * 60, //12 hours
	"trucking_chicago" => 16 * 60 * 60, //16 hours
	"trucking_boston" => 24 * 60 * 60 //24 hours		   
	);

	$witheredError = 3; //The amount of extra time the server will give to a crop before it withers
	//==========================================================================
	$yesterday = time() - 86400;
	$NoTrainIfFull = 0;
	$StopRTrain = 0;
	$NoShipIfFull = 0;
	$StopRShip = 0;
	if(($bot->MaxGoods * 0.9) <= $bot->Goods)
	{
		$bot->SendMsg('> Goods storage > 90% or Full. ' . $bot->Goods . "/" . $bot->MaxGoods);
		$full = "Y";
	}
	else     {$full = "N";}
	if(isset($data->NoTrainIfFull))     {if($data->NoTrainIfFull == "1" && $full == "Y")        {$data->receiveTrain = 0;}}
	if(isset($data->NoShipIfFull))     {if($data->NoShipIfFull == "1" && $full == "Y")        {$data->receiveShip = 0;}}

	// Cruise Ships==============================================================
	if(isset($data->collectCruise))
	{
		if($data->collectCruise == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Cruise] Ship incomings started...');

			if(count($data->collectCruiseIN) == 0)
			$bot->SendMsg(' *** You did not select any ship.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
				foreach ($data->collectCruiseIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "HarvestableShip")
					{
						if($obj["state"] == "grown")
						{
							$bot->SendMsg('[IM-EXPORT-Cruise] Incoming: ' . $obj['contractName']);
							$bot->harvestAction($obj, "harvest", "Cruiser");
							$reload++;
							//check for streakbonus
							if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
							$bot->streakBonus3();					 
						}
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Cruise] No energy to unload the Ships');
			}

			$bot->SendMsg('[IM-EXPORT-Cruise] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//==========================================================================
	if(isset($data->sentCruise) && isset($data->cruiseMissionIN))
	{
		if($data->sentCruise == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('[IM-EXPORT-Cruise] Action started...');
			
			if(count($data->collectCruiseIN) == 0)
			$bot->SendMsg(' *** You did not select any ship.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectCruiseIN as $id) //collect in rounds
			{
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "HarvestableShip") && ($obj["state"] == "plowed"))
				{
					$bot->startContract($obj, "", $data->cruiseMissionIN, "Cruiser");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Cruise] Action done.');
		}
	} 
	//  Truck ============================================================================
	if(isset($data->collectTruck))
	{
		if($data->collectTruck == "1")
		{
		if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Truck] Trucks incomings started...');

			if(count($data->collectTruckIN) == 0)
			$bot->SendMsg(' *** You did not select any Truck.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
			    $bot->GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
				foreach ($data->collectTruckIN as $id) //collect in rounds
				{
					$obj = $bot->dt_fobjects["w$id"];
					if($obj["className"] == "Truck")
					{
						//$contract = $obj["mechanicData"]["contract"]["contractName"];
						//$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						//$timeReady = $contractStarted + $arcargoContracts[$contract];
						//$timeWithered = $contractStarted + $witheredError*$arcargoContracts[$contract];
						
						//if($obj["state"] == "planted"){
							//if ($timeReady < time() && $timeWithered > time()){
							if($obj["state"] == "planted"){ 
							if($clientEnqueueTime >= $obj['mechanicData']['contract']['plantTime'] + (float)$bot->xmlsOb->GetgrowTime($obj['mechanicData']['contract']['contractName']) * 82800.1)
							 { 
								$bot->SendMsg('[IM-EXPORT-Truck] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Truck");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						  }
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Truck] No energy to unload the Planes');
			}

			$bot->SendMsg('[IM-EXPORT-Truck] Action done.');
			if($reload > 0)   { $bot->dt_fobjects = $bot->LoadWorldObjects("world_downtown"); }
		}
	}
	//========================================================================== 
	if(isset($data->clearTruck))
	{
		if($data->clearTruck == "1")
		{
		if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Truck]  Action started...');

			if(count($data->collectTruckIN) == 0)
			$bot->SendMsg(' *** You did not select any Truck.');
			
				//foreach($bot->fobjects as $obj)
				foreach ($data->collectTruckIN as $id) //collect in rounds
				{
					$obj = $bot->dt_fobjects["w$id"];
					if($obj["className"] == "Truck")
					{
						$contract = $obj["mechanicData"]["contract"]["contractName"];
						$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						$timeReady = $contractStarted + $artruckContracts[$contract];
						$timeWithered = $contractStarted + $witheredError*$artruckContracts[$contract];	
						
						if($obj["state"] == "planted"){
						  if ($contract != "trucking_frankfurt"){
							if ($timeReady < time() && $timeWithered < time()){
								$bot->SendMsg('[IM-EXPORT-Truck] Clear: ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ') [' . $obj['mechanicData']['contract']['contractName']. ']');					 
								$bot->ObjectActionImExport($id, $obj, "contractClear", "GMPlay", "clear", $obj['mechanicData']['contract']['contractName'], "Truck");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						  }
						}
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
						}
					}
				}//foreach

			$bot->SendMsg('[IM-EXPORT-Truck] Action done.');
			if($reload > 0)   { $bot->dt_fobjects = $bot->LoadWorldObjects("world_downtown"); }
		}
	}
	//========================================================================== 
	if(isset($data->sentTruck) && isset($data->truckingContractIN))
	{
		if($data->sentTruck == "1")
		{
		if ($bot->currentWorld != "world_downtown") $bot->LoadWorldObjects("world_downtown");
			$reload = 0;
			$bot->SendMsg('[IM-EXPORT-Truck] Action started...');
			
			if(count($data->collectTruckIN) == 0)
			$bot->SendMsg(' *** You did not select any Truck.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectTruckIN as $id) //collect in rounds
			{
				$obj = $bot->dt_fobjects["w$id"];
				if(($obj["className"] == "Truck") && ($obj["state"] == "plowed"))
				{
					$bot->ObjectActionImExport($id, $obj, "contractPlant", "GMPlant", "plant", $data->truckingContractIN, "Truck");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Truck] Action done.');
		}
	}
	
	//  Airline ============================================================================
	
	
	// ============= Cargo =================================================================
	if(isset($data->collectCargo))
	{
		if($data->collectCargo == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Air] Planes incomings started...');

			if(count($data->collectCargoIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
			    $bot->GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
				foreach ($data->collectCargoIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "Airplane")
					{
						//$contract = $obj["mechanicData"]["contract"]["contractName"];
						//$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						//$timeReady = $contractStarted + $arcargoContracts[$contract];
						//$timeWithered = $contractStarted + $witheredError*$arcargoContracts[$contract];
						
						//if($obj["state"] == "planted"){
							//if ($timeReady < time() && $timeWithered > time()){
							if($obj["state"] == "planted"){ 
							if($clientEnqueueTime >= $obj['mechanicData']['contract']['plantTime'] + (float)$bot->xmlsOb->GetgrowTime($obj['mechanicData']['contract']['contractName']) * 82800.0)
							{
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						  }
						  else
							 if($obj["state"] == "grown"){
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							  }
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Air] No energy to unload the Planes');
			}

			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//========================================================================== 
	if(isset($data->clearCargo))
	{
		if($data->clearCargo == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Air]  Action started...');

			if(count($data->collectCargoIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
				//foreach($bot->fobjects as $obj)
				foreach ($data->collectCargoIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "Airplane")
					{
						$contract = $obj["mechanicData"]["contract"]["contractName"];
						$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						$timeReady = $contractStarted + $arcargoContracts[$contract];
						$timeWithered = $contractStarted + $witheredError*$arcargoContracts[$contract];	
						
						if($obj["state"] == "planted"){
							if ($timeReady < time() && $timeWithered < time()){
								$bot->SendMsg('[IM-EXPORT-Plane] Clear: ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ') [' . $obj['mechanicData']['contract']['contractName']. ']');					 
								$bot->ObjectActionImExport($id, $obj, "contractClear", "GMPlay", "clear", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						}
						else
					        if($obj["state"] == "grown"){
							if ($timeReady < time() && $timeWithered < time()){
								$bot->SendMsg('[IM-EXPORT-Plane] Clear: ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ') [' . $obj['mechanicData']['contract']['contractName']. ']');					 
								$bot->ObjectActionImExport($id, $obj, "contractClear", "GMPlay", "clear", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						}
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
						}
					}
				}//foreach

			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//========================================================================== 
	if(isset($data->sentCargo) && isset($data->airplaneMissionCargoIN))
	{
		if($data->sentCargo == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('[IM-EXPORT-Air] Action started...');
			
			if(count($data->collectCargoIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectCargoIN as $id) //collect in rounds
			{
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Airplane") && ($obj["state"] == "plowed"))
				{
					$bot->ObjectActionImExport($id, $obj, "contractPlant", "GMPlant", "plant", $data->airplaneMissionCargoIN, "Airline");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
		}
	}
	// ============= Passenger =================================================================
	if(isset($data->collectPassenger))
	{
		if($data->collectPassenger == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Air] Planes incomings started...');

			if(count($data->collectPassengerIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
				$bot->GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
				foreach ($data->collectPassengerIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "Airplane")
					{
						//$contract = $obj["mechanicData"]["contract"]["contractName"];
						//$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						//$timeReady = $contractStarted + $arpassengerContracts[$contract];
						
						//if($obj["state"] == "planted"){
						//if ($timeReady < time()){
						if($obj["state"] == "planted"){ 
						if($clientEnqueueTime >= $obj['mechanicData']['contract']['plantTime'] + (float)$bot->xmlsOb->GetgrowTime($obj['mechanicData']['contract']['contractName']) * 82800.0)
						    {						
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						}
						else
							 if($obj["state"] == "grown"){
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							  }
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Air] No energy to unload the Planes');
			}

			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//========================================================================== 
	if(isset($data->sentPassenger) && isset($data->airplaneMissionPassengerIN))
	{
		if($data->sentPassenger == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('[IM-EXPORT-Air] Action started...');
			
			if(count($data->collectPassengerIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectPassengerIN as $id) //collect in rounds
			{
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Airplane") && ($obj["state"] == "plowed"))
				{
					$bot->ObjectActionImExport($id, $obj, "contractPlant", "GMPlant", "plant", $data->airplaneMissionPassengerIN, "Airline");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
		}
	}  
	// ============= Holiday =================================================================
	if(isset($data->collectVacation))
	{
		if($data->collectVacation == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Air] Planes incomings started...');

			if(count($data->collectVacationIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
				$bot->GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
				foreach ($data->collectVacationIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "Airplane")
					{
						//$contract = $obj["mechanicData"]["contract"]["contractName"];
						//$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						//$timeReady = $contractStarted + $arvacationContracts[$contract];	
						
						//if($obj["state"] == "planted"){
							//if ($timeReady < time()){
						if($obj["state"] == "planted"){ 
						if($clientEnqueueTime >= $obj['mechanicData']['contract']['plantTime'] + (float)$bot->xmlsOb->GetgrowTime($obj['mechanicData']['contract']['contractName']) * 82800.0)
						    {	
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						}
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Air] No energy to unload the Planes');
			}

			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//========================================================================== 
	if(isset($data->sentVacation) && isset($data->airplaneMissionVacationIN))
	{
		if($data->sentVacation == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('[IM-EXPORT-Air] Action started...');
			
			if(count($data->collectVacationIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectVacationIN as $id) //collect in rounds
			{
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Airplane") && ($obj["state"] == "plowed"))
				{
					$bot->ObjectActionImExport($id, $obj, "contractPlant", "GMPlant", "plant", $data->airplaneMissionVacationIN, "Airline");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
		}
	}
		// ============= Private Jet =================================================================
	if(isset($data->collectPrivate))
	{
		if($data->collectPrivate == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Air] Planes incomings started...');

			if(count($data->collectPrivateIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				//foreach($bot->fobjects as $obj)
				$bot->GetTimers($stamp, $enqueueTime, $clientEnqueueTime);
				foreach ($data->collectPrivateIN as $id) //collect in rounds
				{
					$obj = $bot->fobjects["w$id"];
					if($obj["className"] == "Airplane")
					{
						//$contract = $obj["mechanicData"]["contract"]["contractName"];
						//$contractStarted = $obj["mechanicData"]["contract"]["plantTime"];
						
						//$timeReady = $contractStarted + $arvacationContracts[$contract];	
						
						//if($obj["state"] == "planted"){
							//if ($timeReady < time()){
						if($obj["state"] == "planted"){ 
						if($clientEnqueueTime >= $obj['mechanicData']['contract']['plantTime'] + (float)$bot->xmlsOb->GetgrowTime($obj['mechanicData']['contract']['contractName']) * 82800.0)
						    {
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							}
						}
						     else
							 if($obj["state"] == "grown"){
								$bot->SendMsg('[IM-EXPORT-Air] ' . $obj['itemName'] . ' (ID:' . $obj["id"] . ')  Incoming: ' . $obj['mechanicData']['contract']['contractName']);					 
								$bot->ObjectActionImExport($id, $obj, "contractHarvest", "GMPlay", "harvest", $obj['mechanicData']['contract']['contractName'], "Airline");
								$reload++;
								//check for streakbonus
								if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
								$bot->streakBonus3();
							  }
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Air] No energy to unload the Planes');
			}

			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//========================================================================== 
	if(isset($data->sentPrivate) && isset($data->airplaneMissionPrivateIN))
	{
		if($data->sentPrivate == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$reload = 0;
			$bot->SendMsg('[IM-EXPORT-Air] Action started...');
			
			if(count($data->collectPrivateIN) == 0)
			$bot->SendMsg(' *** You did not select any Plane.');
			
			//foreach($bot->fobjects as $obj)
			foreach ($data->collectPrivateIN as $id) //collect in rounds
			{
				$obj = $bot->fobjects["w$id"];
				if(($obj["className"] == "Airplane") && ($obj["state"] == "plowed"))
				{
					$bot->ObjectActionImExport($id, $obj, "contractPlant", "GMPlant", "plant", $data->airplaneMissionPrivateIN, "Airline");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Air] Action done.');
		}
	} 
	//========================================================================== Trains
	$trainavailable = false;
	if(isset($data->receiveTrain))
	{
		if($data->receiveTrain == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Train] Receiving started...');

			$timeSent = 0;
			$trainName = '';
			$trainflag = false;
			$trainWork = 'w0';
			foreach($bot->train as $train)
			{
				if($train['attributes']['timeSent'] > $timeSent)
				{
					$timeSent = $train['attributes']['timeSent'];
					$trainName = $train['attributes']['trainName'];
				}
			}

			$tripTime = 0;
			if($timeSent > 0)
			{
				$xmlsOb = new xmlsOb();
				foreach($xmlsOb->gsXML->items->item as $item)
				{
					if(($item['type'] == 'train') && ($item['name'] == $trainName))
					{
						$tripTime = $item->trainTripTime;
						break;
					}
				}
				if($timeSent + $tripTime < time())
				{
					$bot->SendMsg('[IM-EXPORT-Train] Train has arrived. Receive train.');
					$bot->receiveTrain($trainWork);
					if(isset($bot->error_msg))
					$bot->ReloadConfig();
					else
					$trainavailable = true;
				}
				else
				$bot->SendMsg('[IM-EXPORT-Train] Train is still in transit.');
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Train] Train has not departed from station.');
				$trainavailable = true;
			}
		}
	}
	//==========================================================================
	if(isset($data->sentTrain))
	{
		if($data->sentTrain == "1" && ($trainavailable || $data->receiveTrain != "1"))
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			// Get how long Train will be Away!

			$mission = $data->trainMissionIN;

			$bot->SendMsg('[IM-EXPORT-Train] Sending train on new mission. (' . $mission . ')');
			$bot->sendTrain($mission);

			// if($qmcount == false) $bot->streamPublish($feed);

			if(isset($bot->error_msg))
			$bot->ReloadConfig();
		}
	}
	//========================================================================== Ferry
	if(isset($data->actionFerry))
	{
		if($data->actionFerry == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Ferry] Action started...');

			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				foreach($bot->fobjects as $obj)
				{
					if($obj["className"] == "FerryShip")
					{
						if($obj["mechanicData"]["harvestState"]["state"] == "supplied")
						{
							$bot->SendMsg('[IM-EXPORT-Ferry] ' . $obj['itemName'] . ': Harvesting...');
							//Collect
							$bot->NewObjectAction($obj, "harvestState", "GMPlay", "harvest", "Ferry");
							if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
							$bot->streakBonus3();
						}
						//Supplying
						$bot->SendMsg('[IM-EXPORT-Ferry] ' . $obj['itemName'] . ': Supplying... (with goods)');
						$bot->NewObjectAction($obj, "supplyState", "GMPlay", "supply", "Ferry");
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Ferry] No energy to unload the Ships');
			}

			$bot->SendMsg('[IM-EXPORT-Ferry] Action done.');
			//$bot->ReloadConfig();
		}
	}    
	//==========================================================================
	if(isset($data->receiveShip))
	{
		if($data->receiveShip == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('=====================================');
			$bot->SendMsg('[IM-EXPORT-Ships] Ship incomings started...');

		    $reload = 0;
			if($bot->energy <= 0)
			$bot->CheckEnergy();//Check if has energy on iventory;
			if($bot->energy > 0)
			{
				foreach($bot->fobjects as $obj)
				{
					if($obj["className"] == "Ship")
					{
						//fix by Tonycell
						//Now work with Eternal Fountain
						if(($obj["state"] == "withered") && ($bot->EternalFountain == true))                    {$obj["state"] = "grown";}

						if($obj["state"] == "grown")
						{
							$bot->SendMsg('[IM-EXPORT-Ships] Incoming: ' . $obj['contractName']);
							$bot->harvestAction($obj, "harvest", "Ship");
							$reload++;
							if(isset($data->ALLbonus) && ($data->ALLbonus == "1"))
							$bot->streakBonus3();
						}
						if(isset($bot->error_msg))
						{
							$bot->ReloadConfig();$reload = 0;
							$hasenergy = $bot->CheckEnergy();
							if(($hasenergy == 0) && ($bot->Energy == 0))                       {break;}
						}
					}
				}//foreach
			}
			else
			{
				$bot->SendMsg('[IM-EXPORT-Ships] No energy to unload the Ships');
			}

			$bot->SendMsg('[IM-EXPORT-Ships] Action done.');
			if($reload > 0)   { $bot->fobjects = $bot->LoadWorldObjects("world_main"); }
		}
	}
	//==========================================================================
	if(isset($data->sentShip) && isset($data->shipMissionIN))
	{
		if($data->sentShip == "1")
		{
		if ($bot->currentWorld != "world_main") $bot->LoadWorldObjects("world_main");
			$bot->SendMsg('[IM-EXPORT-Ships] Send Ship starting, mission: ' . $data->shipMissionIN);
			foreach($bot->fobjects as $obj)
			{
				if(($obj["className"] == "Ship") && ($obj["state"] == "plowed"))
				{
					$bot->startContract($obj, "", $data->shipMissionIN, "Ship");
				}
				if(isset($bot->error_msg))              {$bot->ReloadConfig();}
			}
			$bot->SendMsg('[IM-EXPORT-Ships] Action done.');
		}
	}
	//==========================================================================
}
$this->AddHook('start_im-export', 'ImExport');
?>